package cn.iocoder.yudao.module.dr.controller.admin.sourcetag;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dr.controller.admin.source.vo.SourceRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourcetag.vo.SourceTagPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourcetag.vo.SourceTagRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourcetag.vo.SourceTagSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.source.SourceDO;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourcetag.SourceTagDO;
import cn.iocoder.yudao.module.dr.service.sourcetag.SourceTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 标签管理")
@RestController
@RequestMapping("/dr/source-tag")
@Validated
public class SourceTagController {

    @Resource
    private SourceTagService sourceTagService;

    @PostMapping("/create")
    @Operation(summary = "创建标签管理")
    @PreAuthorize("@ss.hasPermission('dr:source-tag:create')")
    public CommonResult<Long> createSourceTag(@Valid @RequestBody SourceTagSaveReqVO createReqVO) {
        return success(sourceTagService.createSourceTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签管理")
    @PreAuthorize("@ss.hasPermission('dr:source-tag:update')")
    public CommonResult<Boolean> updateSourceTag(@Valid @RequestBody SourceTagSaveReqVO updateReqVO) {
        sourceTagService.updateSourceTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:source-tag:delete')")
    public CommonResult<Boolean> deleteSourceTag(@RequestParam("id") Long id) {
        sourceTagService.deleteSourceTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签管理")
    @Parameter(name = "id", description = "编号", required = false, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:source-tag:query')")
    public CommonResult<SourceTagRespVO> getSourceTag(@RequestParam(name = "id", required = false) Long id) {
        if(Objects.isNull(id)){
            return success(null);
        }
        SourceTagDO sourceTag = sourceTagService.getSourceTag(id);
        SourceTagRespVO respVO = BeanUtils.toBean(sourceTag, SourceTagRespVO.class);
        if(Objects.nonNull(sourceTag) && Objects.nonNull(sourceTag.getId())){
            List<SourceDO> sourceDOS = sourceTagService.selectRef(sourceTag.getId());
            respVO.setSourceRespVOList(BeanUtils.toBean(sourceDOS, SourceRespVO.class));
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签管理分页")
    @PreAuthorize("@ss.hasPermission('dr:source-tag:query')")
    public CommonResult<PageResult<SourceTagRespVO>> getSourceTagPage(@Valid SourceTagPageReqVO pageReqVO) {
        Long userId = getLoginUserId();
        PageResult<SourceTagDO> pageResult = sourceTagService.getSourceTagPage(userId,pageReqVO);
        return success(BeanUtils.toBean(pageResult, SourceTagRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签管理 Excel")
    @PreAuthorize("@ss.hasPermission('dr:source-tag:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSourceTagExcel(@Valid SourceTagPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SourceTagDO> list = sourceTagService.getSourceTagPage(getLoginUserId(),pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签管理.xls", "数据", SourceTagRespVO.class,
                        BeanUtils.toBean(list, SourceTagRespVO.class));
    }

}